package AutoGen;



import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class StudentGen {

    public StudentGen() {
    }

    /**
     * Generates random students
     * @param path path to file with random generated names
     */
    @Test
    public void studentGen(String path)
    {
        ArrayList<String> lines = new ArrayList<String>();
        int startIndex = 0;
        int studentNo = 0;
        int phone = 0;

        try{
            FileInputStream fstream = new FileInputStream(path);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null)   {
                lines.add(strLine);
            }
            in.close();
        }catch (Exception e){
            System.err.println("Error: " + e.getMessage());
        }

        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("StudentAutogen.sql"));
            for (int i = 0; i < lines.size(); i++) {
                studentNo = randInt(100000,999999);
                phone = randInt(1000, 100000000);
                String name = lines.get(i);
                String student = "INSERT INTO Student (studentId, studentNo, teamId) VALUES ( " + startIndex + ", " + studentNo + ", 1)";
                String person = "INSERT INTO Person (id, name, username, password, address, email, phoneNo, type) VALUES (" + startIndex + ",\'" + name + "\'," + studentNo + ",\'password\',\'H. C. Andersensvej " + startIndex + " 9000 Aalborg\',\'" + studentNo + "@ucn.dk\'," + phone + ",\'Student\')";
                out.write(student + "\n");
                out.newLine();
                out.write(person + "\n");
                out.newLine();
                System.out.println(student +"\n"+person);
                startIndex++;
            }
            out.close();
        } catch (IOException e) {}

    }


    public static int randInt(int min, int max) {
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
}
